package com.gxg.config;

import com.gxg.config.code.RCode;

import java.util.HashMap;

/**
 * 返回数据
 *
 */
public class R extends HashMap<String, Object> {
	private static final long serialVersionUID = 1L;

	private R rson;

	public R() {
		put("code", RCode.SUCCESS);
		put("message",RCode.SUCCESS_MSG);
	}
	public R(boolean sign) {
	}

	/**
	 * 返回位置异常错误 8000
	 * @return
	 */
	public static R error() {
		return error(RCode.UNKNOW_ERR, RCode.UNKNOW_ERR_MSG);
	}

	/**
	 * 返回异常信息，错误信息自定义
	 * @param code
	 * @param msg
	 * @return
	 */
	public static R error( String code, String msg) {
		R r = new R();
		r.put("code", code);
		r.put("message", msg);
		return r;
	}
	
	/**
	 * 返回成功，成功信息自定义
	 * @param code
	 * @param msg
	 * @return
	 */
	public static R success( String code, String msg) {
		R r = new R();
		r.put("code", code);
		r.put("message", msg);
		return r;
	}

	/**
	 * 返回异常信息，错误信息为缺省值
	 * @param code
	 * @return
	 */
	public static R error( String code) {
		R r = new R();
		r.put("code", code);
		//r.put("message", RCode.class.get);
		return r;
	}

	public R ok( String key , Object value) {
		 if(this.rson==null){
			 rson = new R(false);
		 }
		rson.put(key,value);
		this.put("data",rson);
		return this;
	}

	public static R ok() {
		return new R();
	}

	@Override
	public R put( String key, Object value) {
		super.put(key, value);
		return this;
	}

}
